package com.epoint.androidphone.mobileoa.ui.todo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.epoint.androidmobile.bizlogic.todo.model.NextStepInfoModel;
import com.epoint.androidmobile.bizlogic.todo.model.UserInfoModel;
import com.epoint.androidmobile.core.control.ToastUtil;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.ui.contacts.ContactsListView;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;
import com.epoint.androidmobile.bizlogic.todo.task.Handle_PostAuditOpinion_Task;

/**
 * @author liyc
 * @time 2013-5-8 下午7:06:11
 * @annomation 下一步处理
 */
public class TodoNextStepView extends SuperPhonePage {
	private TextView task_progress,title1;
	private Button opitionButton,cleanButton,deal_btn;
	private EditText etPersonChoose,task_opinion;
	private ImageView ivDeletePersonChoose,ivPersonChoose;
	long submitOpinionTaskID;
	NextStepInfoModel nm;

	List<UserInfoModel> chooseFilterUser = new ArrayList<UserInfoModel>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContentViewAndTitle(R.layout.wsnextstepsignview, "事项处理");
		cleanButton = (Button) findViewById(R.id.cleanButton);
		cleanButton.setOnClickListener(this);
		deal_btn = (Button) findViewById(R.id.deal_btn);
		deal_btn.setOnClickListener(this);
		task_opinion = (EditText) findViewById(R.id.task_opinion);
		opitionButton = (Button) findViewById(R.id.opitionButton);
		opitionButton.setOnClickListener(this);
		ivDeletePersonChoose = (ImageView) findViewById(R.id.ivDeletePersonChoose);
		ivDeletePersonChoose.setOnClickListener(this);
		etPersonChoose = (EditText) findViewById(R.id.etPersonChoose);
		ivPersonChoose = (ImageView) findViewById(R.id.ivPersonChoose);
		ivPersonChoose.setOnClickListener(this);
		title1 = (TextView) findViewById(R.id.title1);
		etPersonChoose.setTag("");

		nm = (NextStepInfoModel) getIntent().getSerializableExtra("NextStepInfoModel");
		title1.setText(Html.fromHtml("下一步<font color=red>【"+nm.StepName+"】</font>处理人"));

//		ToastUtil.toastLong(this, "默认处理人："+nm.defaultHandleUserList.size());
//		ToastUtil.toastLong(this, "处理人范围："+nm.filterHandleUserList.size());

		/*UserInfoModel ui = new UserInfoModel();
		ui.UserGuid = "123";
		ui.UserName = "张三";

		nm.filterHandleUserList.add(ui);

		ui = new UserInfoModel();
		ui.UserGuid = "1234";
		ui.UserName = "李四";

		nm.filterHandleUserList.add(ui);*/

		if(nm.CanEditUser.equals("0"))
		{
			ivDeletePersonChoose.setVisibility(View.GONE);
			ivPersonChoose.setVisibility(View.GONE);
			if(nm.defaultHandleUserList.size()==0)
			{
				etPersonChoose.setVisibility(View.GONE);
			}
		}
		if(nm.defaultHandleUserList.size()>0)
		{
			for(UserInfoModel u:nm.defaultHandleUserList)
			{
				etPersonChoose.setText(u.UserName+";");
				etPersonChoose.setTag(u.UserGuid+";");
			}
		}


	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if(v==deal_btn)
		{
			if(etPersonChoose.getVisibility()==View.VISIBLE){
				if(etPersonChoose.getText().toString().trim().length()==0)
				{
					ToastUtil.toastWorning(this, "请选择下一步处理人！");
					return;
				}
			}

			showDialogMiddle("正在提交...");
			HashMap<String, Object> params = getPassMap();
			params.put("NextPersonGuid", etPersonChoose.getTag());
			params.put("Opinion", task_opinion.getText());
			params.put("MessageItemGuid", getIntent().getStringExtra("MessageItemGuid"));
			params.put("NextStepGuid", nm.StepGuid);
			params.put("PDFContentPath", "");
			submitOpinionTaskID = new Handle_PostAuditOpinion_Task(this, params).startTask();
		}
		else if(v==ivPersonChoose)
		{

			if(nm.filterHandleUserList.size()>0)
			{
				final String[] names = new String[nm.filterHandleUserList.size()];
				final boolean[] checkedItems = new boolean[nm.filterHandleUserList.size()];

				for(int i=0;i<names.length;i++)
				{
					names[i] = nm.filterHandleUserList.get(i).UserName;
					if(chooseFilterUser.contains(nm.filterHandleUserList.get(i)))
					{
						checkedItems[i] = true;
					}
				}

				Builder builder=new AlertDialog.Builder(this);
				builder.setTitle("选择处理人");
				builder.setMultiChoiceItems(names, checkedItems, new OnMultiChoiceClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which, boolean isChecked) {
						if(isChecked)
						{
							if(!chooseFilterUser.contains(nm.filterHandleUserList.get(which)))
							{
								chooseFilterUser.add(nm.filterHandleUserList.get(which));
							}
						}
						else{
							if(chooseFilterUser.contains(nm.filterHandleUserList.get(which)))
							{
								chooseFilterUser.remove(nm.filterHandleUserList.get(which));
							}
						}
					}
				});
				builder.setPositiveButton("确定", new OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						String names = "";
						String guids = "";
						for(int i=0;i<chooseFilterUser.size();i++)
						{
							names += chooseFilterUser.get(i).UserName+";";
							guids += chooseFilterUser.get(i).UserGuid+";";
						}
						etPersonChoose.setText(names);
						etPersonChoose.setTag(guids);
					}

				});
				builder.setNegativeButton("取消", null);
				AlertDialog dialog=builder.create();
				dialog.show();
				return;
			}

			Intent intent = new Intent(this, ContactsListView.class);
			intent.putExtra("enterflag", 2);
			intent.putExtra("EnterClassName", this.getClass().getName());
			startActivityForResult(intent, 1001);
		}
		else if(v==ivDeletePersonChoose)
		{
			etPersonChoose.setText("");
			etPersonChoose.setTag("");
			chooseFilterUser.clear();
		}
	}

	@Override
	public void refreshUI(long taskId, Object obj) {
		super.refreshUI(taskId, obj);
		if(taskId==submitOpinionTaskID)
		{
			if (validateXML(obj)) {
				System.out.println(obj.toString());
				ToastUtil.toastShort(this, "处理成功");
				setResult(RESULT_OK);
				finish();
			}
		}
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==1001){
			switch (resultCode) {
			case RESULT_OK:
				etPersonChoose.setText(data.getStringExtra("names"));
				etPersonChoose.setTag(data.getStringExtra("guids"));
				break;
			default:
				break;
			}
		}
	}
}
